﻿#include "schoolview.h"
#include <QSlider>
#include <QGridLayout>
#include <QFile>
#include <QTextStream>
#include <QGraphicsScene>
#include <QTimer>
#include <QDebug>

#include "npc.h"
#include "math.h"
void schoolview::timerEvent(QTimerEvent *)//时刻运行
{



//    if(mm->mark<=0)
//    {
//        timer->stop();
//        this->hide();
//        emit over();
//        killTimer(0);

//    }

    //显示速度
    vCoord->setText(QString::number(mm->vx)+","+QString::number(mm->vy)+"->"+QString::number(mm->v()));
    vCoord->setStyleSheet("color:yellow");

    //显示坐标
    xCoord->setText(QString::number(mm->startX)+","+QString::number(mm->startY));
    xCoord->setStyleSheet("color:yellow");

    //显示分数
    mCoord->setText(QString::number(mm->mark));
    mCoord->setStyleSheet("color:yellow");
    li.setLine(mm->startX,mm->startY,mm->startX+mm->vx*50,mm->startY+mm->vy*50);

    pen.setColor (Qt::red);
    pen.setStyle (Qt::SolidLine);
     pen.setWidthF (10);
     li.setPen (pen);
     li.setFlag (QGraphicsItem::ItemIsMovable);
    //简单处理
    gameLoop();
    //玩家控制的物体碰撞检测
    for(int i=0;i<n;i++)
    {
    if(mm->collidesWithItem(kg+i)&&(kg+i)->live)
    {
        mm->mark++;

        if(mm->vx-(kg+i)->vx>0.001)//防止速度相似的物体反复交换速度造成物体粘连
       qSwap(mm->vx,(kg+i)->vx);//假设质量相等，速度交换，动能动量守恒
        if(mm->vy-(kg+i)->vy>0.001)

       qSwap(mm->vy,(kg+i)->vy);


    }

}

    //自动物体碰撞检测
    for(int j=0;j<n;j++)
    {
        bool ok=true;

        for(int i=0;i<n;i++)
        {
        if((kg+j)->collidesWithItem(kg+i)&&ok&&(kg+i)->live&&(kg+j)->live)
            {
            //ok=false;
            //qDebug()<<i<<" "<<j;
            //mm->mark++;

            if((kg+j)->vx-(kg+i)->vx<=0.001)
            qSwap((kg+j)->vx,(kg+i)->vx);
            if((kg+j)->vy-(kg+i)->vy<=0.001)

            qSwap((kg+j)->vy,(kg+i)->vy);
            ok=false;
            break;
            }
        if(!ok)
            break;//bug记录
        }
    }
    for(int i=0;i<n;i++)
    {
        //delete (kg+i);

        if((kg+i)->collidesWithItem(o1)&&(kg+i)->live)
        {
            //(kg+i)->live=false;
//                (kg+i)->vx=qrand()%5;//初速度随机
//                (kg+i)->vy=qrand()%5;

//                (kg+i)->fk=0.001;//阻力常数设定
//                (kg+i)->setPos(qrand()%200-200,qrand()%200-200);//位置随机

//
            //kg->live=false;

            if(qrand()%4==1)
            mm->mark++;

            break;



        }
    }

    if(it==balitai)
    {
//        if(mm->mark<=0)
//        {
//            timer->stop();
//            this->hide();
//            emit over();
//            killTimer(0);

//        }

    }
    if(it==jinnan)
    {
//        are+=0.01;
//        qDebug()<<are;
        rotate(0.01);


    }
    if(it==taida)
    {
        rotate(mm->v()/500);

    }
}
schoolview::schoolview()
{

}
void schoolview::playGame()
{
    int width = map.width();
    int height = map.height();
    scene->setSceneRect(-width/2,-height/2,width,height);
    setScene(scene);//视图观察场景

    startTimer(10);//开启视图计时器


    timer = new QTimer(this);//场景的计时器
    connect(timer,SIGNAL(timeout()),scene,SLOT(advance()));//链接到场景的时刻运行中
    timer->start(10);

    setCacheMode(CacheBackground);//加载缓存

    npc *op=new npc[5];//??????
        for(int i=0;i<3;i++)
        {
            (op+i)->setPos(qrand()%400,qrand()%400);
            scene->addItem(op+i);
        }
    kg=new player[n];//循环生成n个自动物体
    for(int i=0;i<n;i++)
    {
        (kg+i)->pix_up.load(":/RES/rrrrrr.png");
        (kg+i)->pix_up=(kg+i)->pix_up.scaled(100,100);
        (kg+i)->vx=qrand()%5;//初速度随机
        (kg+i)->vy=qrand()%5;

        (kg+i)->fk=0.001;//阻力常数设定
        (kg+i)->setPos(qrand()%500,qrand()%500);//位置随机
        scene->addItem(kg+i);//加入场景
    }

    //生成玩家控制的物体
    mm=new player;
    mm->pix_up.load(":/RES/redalpaly.png");
    mm->pix_up=mm->pix_up.scaled(100,100);
    scene->addItem (&li);


    mm->setPos(0,0);//初始位置
    scene->addItem(mm);

}

void schoolview::last()//显示页面，开始游戏
{
    this->show();
    playGame();

}
void schoolview::initialization(school cho)
{
    //rotate(0);

    setWindowIcon(QIcon(":/RES/qqqqwwee.png"));


    scene = new QGraphicsScene(this);//构造场景



    //初始化背景
    if(cho==balitai)
    {
        //mm->mark=10;

        it=cho;
        map.load(":/RES/1map.jpg");
        map=map.scaled(1600,900);
//        qDebug()<<map.width();
//        qDebug()<<map.height();


        o1=new npc;
        o1->pix.load(":/RES/qqq.png");
        o1->pix=o1->pix.scaled(25,25);
        o1->setPos(100,100);
        //scene->addItem(o1);


    }
    if(cho==jinnan)
    {
        it=jinnan;

        map.load(":/RES/milk.jpg");
        map=map.scaled(700,700);

    }
    if(cho==taida)
    {
        it=taida;

        map.load(":/RES/wwwk.jpg");
        map=map.scaled(800,450);

    }

    zoom=50;
    //用于地图缩放滑动条									//(c)
    QSlider *slider = new QSlider;
    slider->setOrientation(Qt::Vertical);
    if(it==jinnan)
        slider->setRange(1,500);
      else
        slider->setRange(100,500);

    slider->setTickInterval(10);
    slider->setValue(100);
    connect(slider,SIGNAL(valueChanged(int)),this,SLOT(slotZoom (int)));
    QLabel *zoominLabel = new QLabel;
    zoominLabel->setScaledContents(true);
    QLabel *zoomoutLabel = new QLabel;
    zoomoutLabel->setScaledContents(true);
    //坐标值显示区
    QLabel *label1 = new QLabel(tr("速度"));
    vCoord = new QLabel;

    label1->setStyleSheet("color:yellow");

    QLabel *label2 = new QLabel(tr("位置"));
    label2->setStyleSheet("color:yellow");

    xCoord = new QLabel;
    QLabel *label3 = new QLabel(tr("分数"));
    label3->setStyleSheet("color:yellow");

    mCoord = new QLabel;
    //坐标显示区布局
    QGridLayout *gridLayout = new QGridLayout;
    gridLayout->addWidget(label1,0,0);
    gridLayout->addWidget(vCoord,0,1);
    gridLayout->addWidget(label2,1,0);
    gridLayout->addWidget(xCoord,1,1);
    gridLayout->addWidget(label3,2,0);
    gridLayout->addWidget(mCoord,2,1);
    gridLayout->setSizeConstraint(QLayout::SetFixedSize);
    QFrame *coordFrame = new QFrame;
    coordFrame->setLayout(gridLayout);
    //缩放控制子布局
    QVBoxLayout *zoomLayout = new QVBoxLayout;
    zoomLayout->addWidget(slider);
    //坐标显示区域布局
    QVBoxLayout *coordLayout = new QVBoxLayout;
    coordLayout->addWidget(coordFrame);
    coordLayout->addStretch();
    //主布局
    QHBoxLayout *mainLayout = new QHBoxLayout;
    mainLayout->addLayout(zoomLayout);
    mainLayout->addLayout(coordLayout);
    mainLayout->addStretch();
    mainLayout->setMargin(30);
    mainLayout->setSpacing(10);
    setLayout(mainLayout);
    if(it==jinnan)
    {
    setWindowTitle("迷航夜空");

    setMinimumSize(map.width()*1.414,map.height()*1.414);
    setMaximumSize(map.width()*1.414,map.height()*1.414);
    }
    if(it==balitai)
    {
    setWindowTitle("迷航星海");

    setMinimumSize(map.width(),map.height());
    setMaximumSize(map.width(),map.height());
    }
    if(it==taida)
    {
        setWindowTitle("扭曲时空");

        setMinimumSize(map.width()*2,map.height()*2);
        setMaximumSize(map.width()*2,map.height()*2);

    }



}
void schoolview::drawBackground(QPainter *painter, const QRectF &rect)
{
    painter->drawPixmap(int(sceneRect().left()),int(sceneRect().top()), map);
}

void schoolview::slotZoom(int value)             //地图缩放
{
    qreal s;
    if(value>zoom)                              //放大
    {
        s=pow(1.01,(value-zoom));
    }
    else                                        //缩小
    {
        s=pow(1/1.01,(zoom-value));
    }
    scale(s,s);
    //rotate(s);
    //setDragMode(QGraphicsView::RubberBandDrag);
    zoom = value;
}
void schoolview::gameLoop()//简单处理
{
    //bool量实现外力
    if (m_upKeyPressed)
        mm->vy -= mm->step;
    if (m_downKeyPressed)
        mm->vy += mm->step;
    if (m_leftKeyPressed)
        mm->vx -= mm->step;
    if (m_rightKeyPressed)
        mm->vx += mm->step;
    //对于各个运动物体进行边界控制
    mm->edge(map.width(),map.height());


    for(int i=0;i<n;i++)
    (kg+i)->edge(map.width(),map.height());

}



//void schoolview::mousePressEvent(QMouseEvent *event)
//{
//    QPoint viewPoint = event->pos();

//    QPointF scenePoint = mapToScene(viewPoint);
//   if(event->button()==Qt::LeftButton&&scenePoint.x()<200&&scenePoint.y()<200)
//    {
//        emit next();

//    }
//}

void schoolview::keyPressEvent(QKeyEvent *event)
{
    if(event->key()==Qt::Key_Space)//长按空格时间暂停
    {
        timer->stop();
    }

    if(event->key()==Qt::Key_B)//长按空格时间暂停
    {
        timer->stop();
        this->hide();
        killTimer(0);
        emit back();
    }

//键盘控制bool量
    switch (event->key())
        {
            case Qt::Key_W:
                m_upKeyPressed = true;
                break;
            case Qt::Key_S:
                m_downKeyPressed = true;
                break;
            case Qt::Key_A:
                m_leftKeyPressed = true;
                break;
            case Qt::Key_D:
                m_rightKeyPressed = true;
                break;
            default:
                break;
        }
}



void schoolview::keyReleaseEvent(QKeyEvent *event)
{
    if(event->key()==Qt::Key_Space)
    {
        timer->start();
    }

    switch (event->key())
       {
           case Qt::Key_W:
               m_upKeyPressed = false;
               break;
           case Qt::Key_S:
               m_downKeyPressed = false;
               break;
           case Qt::Key_A:
               m_leftKeyPressed = false;
               break;
           case Qt::Key_D:
               m_rightKeyPressed = false;
               break;
           default:
               break;
       }


}
